package com.example.xdzx.controller;

import com.example.xdzx.pojo.User;
import com.example.xdzx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    UserService userService;

    /**
     * 登录，登录成功就存入用户信息（ID,用户名
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/login")
    public Map<String,Object> login(@RequestBody User user, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();

        try{
            User user1 = userService.getUserById(user.getUserId());
            if(user.getPassword().equals(user1.getPassword())){
                map.put("isSuccess",true);
                session.setAttribute("username",user1.getUsername());
                session.setAttribute("userId",user1.getUserId());
            }else{
                map.put("isSuccess",false);
                map.put("msg","用户密码错误");
            }
        }catch (NullPointerException e) {
            map.put("isSuccess",false);
            map.put("msg","没有找到该用户");
            return map;
        }
        return map;
    }
}
